package com.zhf.educommentboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhf.eduapi.entity.CourseComment;
import com.zhf.eduapi.entity.CourseCommentFavoriteRecord;
import com.zhf.educommentboot.mapper.CourseCommentDao;
import com.zhf.educommentboot.mapper.CourseCommentFavoriteRecordDao;
import com.zhf.educommentboot.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author: ZhaoHuanfeng
 * @Date: 2021/2/16 12:32
 */
@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CourseCommentDao courseCommentDao;

    @Autowired
    private CourseCommentFavoriteRecordDao courseCommentFavoriteRecordDao;

    @Override
    public Integer saveComment(CourseComment courseComment) {
        return courseCommentDao.insert(courseComment);
    }

    @Override
    public List<CourseComment> getCommentsByCourseId(Integer courseid, Integer offset, Integer pageSize) {
        return courseCommentDao.getCommentsByCourseId(courseid, offset,pageSize);
    }


    /**
     * 点赞
     * 先查看当前用户对这条留言是否点过赞
     * 如果点过，修改isdel状态即可 取消赞
     * 如果没点过，新增一条点赞信息
     * @param comment_id 留言编号
     * @param userid 用户编号
     * @return
     */
    @Override
    public Integer saveFavorite(Integer comment_id, Integer userid) {
//        Integer i = courseCommentDao.existsFavorite(comment_id, userid);
        QueryWrapper<CourseCommentFavoriteRecord> qw=new QueryWrapper<>();
        qw.eq("comment_id", comment_id);
        qw.eq("userid", userid);
        Integer i = courseCommentFavoriteRecordDao.selectCount(qw);
        int i1=0;
        int i2=0;
        //没点过赞
        if (i==0){
//            i1=courseCommentDao.saveCommentFavorite(comment_id, userid);
            CourseCommentFavoriteRecord favoriteRecord = new CourseCommentFavoriteRecord();
            favoriteRecord.setCommentId(comment_id);
            favoriteRecord.setUserId(userid);
            favoriteRecord.setIsDel(0);
            favoriteRecord.setCreateTime(new Date());
            favoriteRecord.setUpdateTime(new Date());
            i1 = courseCommentFavoriteRecordDao.insert(favoriteRecord);
        }else{
            //取消赞
//            i1=courseCommentDao.updateFavoriteStatus(0,comment_id,userid);
            CourseCommentFavoriteRecord favoriteRecord = new CourseCommentFavoriteRecord();
            favoriteRecord.setIsDel(0);
            QueryWrapper<CourseCommentFavoriteRecord> qw2 = new QueryWrapper<>();
            qw2.eq("comment_id", comment_id); // where comment_id = #{cid}
            qw2.eq("user_id", userid); // and user_id = #{uid}
            i1 = courseCommentFavoriteRecordDao.update(favoriteRecord,qw2);
        }
        i2=courseCommentDao.updateLikeCount(1,comment_id);
        if (i1==0 || i2==0){
            throw new RuntimeException("点赞失败！");
        }
        return comment_id;
    }

    /**
     * 删除点赞信息（is_del=1）
     * 更新点赞数量-1
     * @param comment_id
     * @param userid
     * @return
     */
    @Override
    public Integer cancelFavorite(Integer comment_id, Integer userid) {
        CourseCommentFavoriteRecord favoriteRecord = new CourseCommentFavoriteRecord();
        favoriteRecord.setIsDel(1);

        QueryWrapper<CourseCommentFavoriteRecord> qw = new QueryWrapper<>();
        qw.eq("comment_id", comment_id); // where comment_id = #{cid}
        qw.eq("user_id", userid); // and user_id = #{uid}

        Integer i1 = courseCommentFavoriteRecordDao.update(favoriteRecord,qw);

        Integer i2 = courseCommentDao.updateLikeCount(-1,comment_id);

        if(i1 == 0 || i2 == 0){
            throw  new RuntimeException("取消赞失败！");
        }
        return i2;
    }
}
